//
//  E7GameLayer.cpp
//  e7engine
//
//  Created by cc on 12-3-13.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7GameLayer.h"

#include "E7StageData.h"
#include "E7Logic.h"

CE7GameLayer* s_pGameLayer = NULL ;

void SingleTouchedEvent(CCPoint offset, float scale, CCPoint pos)
{
    printf("---- SingleTouchedEvent 1\n") ;
    if (!s_pLogic->IsUserTurn())
    {
        return ;
    }

    s_pLogic->s_bIsRedraw = true ;
    s_pLogic->s_bIsUpdateInfo = true ;

    printf("---- SingleTouchedEvent 2\n") ;
    CE7Cell* pTemp = s_pLogic->ActiveMap()->GetSelectCellInfo(pos) ;
    if (NULL == pTemp || -1 == pTemp->GetCountryID())
    {
//        s_pLogic->s_pSelect = NULL ;
//        if (-1 != s_pLogic->s_eSelUnitID)
//        {
//            CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
//            pUnit->SetShowState(true) ;
//            s_pLogic->s_eSelUnitID = -1 ;
//            s_pLogic->s_eSelectUV = UV_NONE ;
//        }
//        s_pLogic->s_eCreateUV = UV_NONE ;

        return ;
    }

    printf("---- SingleTouchedEvent 2.5\n") ;
    if (0 != pTemp->GetCountryID() && NULL == s_pLogic->s_pSelect)
    {
        return ;
    }

    printf("---- SingleTouchedEvent 3\n") ;
    if (0 == pTemp->GetCountryID() &&
        NULL != s_pLogic->s_pSelect &&
        -1 != pTemp->GetTownID() &&
        s_pLogic->s_pSelect->GetTownID() != pTemp->GetTownID())
    {
        s_pLogic->s_pSelect = pTemp ;
        if (-1 != s_pLogic->s_eSelUnitID)
        {
            CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
            pUnit->SetShowState(true) ;
            s_pLogic->s_eSelUnitID = -1 ;
            s_pLogic->s_eSelectUV = UV_NONE ;
        }
        s_pLogic->s_eCreateUV = UV_NONE ;
        return ;
    }

    printf("---- SingleTouchedEvent 4\n") ;
    // create unit.
    if (UV_NONE != s_pLogic->s_eCreateUV)
    {
        if (UV_NONE != s_pLogic->s_eSelectUV)
        {
            bool res = s_pLogic->GetRule()->AttachWithCreate(s_pLogic->s_eSelUnitID, s_pLogic->s_eCreateUV) ;
            if (res)
            {
                if (0 != pTemp->GetCountryID())
                {
                    // attack cell.
                    bool res = s_pLogic->GetRule()->Attack(0, s_pLogic->s_eSelUnitID, pTemp->GetID()) ;
                    if (res)
                    {
                        // sound.
                        if (s_bPlaySound)
                        {
                            if (-1 == pTemp->GetUnitID())
                            {
                            }
                            else
                            {
                                CE7Unit* pSoundUnit = s_pLogic->GetSelectUnitInfo(pTemp->GetUnitID()) ;
                                if (UV_CITY == pSoundUnit->GetUV())
                                {
                                    SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_city) ;
                                }else if (UV_SOLDIER_1 == pSoundUnit->GetUV() ||
                                         UV_SOLDIER_2 == pSoundUnit->GetUV() ||
                                         UV_SOLDIER_3 == pSoundUnit->GetUV())
                                {
                                    SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_soldier) ;
                                }
                                else // if (UV_TREE == pSoundUnit->GetUV())
                                {
                                    SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (s_bPlaySound)
                        {
                            SimpleAudioEngine::sharedEngine()->playEffect(sound_unconsid) ;
                        }
                    }
                }
                CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
                pUnit->SetShowState(true) ;
                s_pLogic->s_eSelUnitID = -1 ;
                s_pLogic->s_eSelectUV = UV_NONE ;
                s_pLogic->s_eCreateUV = UV_NONE ;
            }
            else
            {
                if (s_bPlaySound)
                {
                    SimpleAudioEngine::sharedEngine()->playEffect(sound_unconsid) ;
                }
            }
        }
        else
        {
            int res = s_pLogic->GetRule()->CreateUnit(0, s_pLogic->s_pSelect->GetTownID(), s_pLogic->s_eCreateUV, pTemp->GetID()) ;
            if (-1 != res)
            {
                if (0 != pTemp->GetCountryID())
                {
                    // sound.
                    if (s_bPlaySound)
                    {
                        if (-1 == pTemp->GetUnitID())
                        {
                            SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                        }
                        else
                        {
                            CE7Unit* pSoundUnit = s_pLogic->GetSelectUnitInfo(pTemp->GetUnitID()) ;
                            if (UV_CITY == pSoundUnit->GetUV())
                            {
                                SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_city) ;
                            }else if (UV_SOLDIER_1 == pSoundUnit->GetUV() ||
                                      UV_SOLDIER_2 == pSoundUnit->GetUV() ||
                                      UV_SOLDIER_3 == pSoundUnit->GetUV())
                            {
                                SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_soldier) ;
                            }
                            else // if (UV_TREE == pSoundUnit->GetUV())
                            {
                                SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                            }
                        }
                    }
                }

                s_pLogic->s_eCreateUV = UV_NONE ;
            }
            else
            {
                if (s_bPlaySound)
                {
                    SimpleAudioEngine::sharedEngine()->playEffect(sound_unconsid) ;
                }
            }
        }
        return ;
    }
    
    printf("---- SingleTouchedEvent 5\n") ;
    if (-1 == pTemp->GetTownID())
    {
        if (-1 != s_pLogic->s_eSelUnitID && 0 != pTemp->GetCountryID())
        {
            // attack cell.
            bool res = s_pLogic->GetRule()->Attack(0, s_pLogic->s_eSelUnitID, pTemp->GetID()) ;
            if (res)
            {
                // sound.
                if (s_bPlaySound)
                {
                    if (-1 == pTemp->GetUnitID())
                    {
                        SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                    }
                    else
                    {
                        CE7Unit* pSoundUnit = s_pLogic->GetSelectUnitInfo(pTemp->GetUnitID()) ;
                        if (UV_CITY == pSoundUnit->GetUV())
                        {
                            SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_city) ;
                        }else if (UV_SOLDIER_1 == pSoundUnit->GetUV() ||
                                  UV_SOLDIER_2 == pSoundUnit->GetUV() ||
                                  UV_SOLDIER_3 == pSoundUnit->GetUV())
                        {
                            SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_soldier) ;
                        }
                        else // if (UV_TREE == pSoundUnit->GetUV())
                        {
                            SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                        }
                    }
                }

                s_pLogic->s_eSelUnitID = -1 ;
                s_pLogic->s_eSelectUV = UV_NONE ;
            }
            else
            {
                if (s_bPlaySound)
                {
                    SimpleAudioEngine::sharedEngine()->playEffect(sound_unconsid) ;
                }
            }
        }
        return ;
    }
    
    printf("---- SingleTouchedEvent 6\n") ;
    if (NULL == s_pLogic->s_pSelect)
    {
        s_pLogic->s_pSelect = pTemp ;
        return ;
    }
    
    printf("---- SingleTouchedEvent 7\n") ;
    if (0 != pTemp->GetCountryID())
    {
        if (-1 == pTemp->GetCountryID())
        {
            return ;
        }

        if (-1 == s_pLogic->s_eSelUnitID && 
            (UV_SOLDIER_1 != s_pLogic->s_eCreateUV && UV_SOLDIER_2 != s_pLogic->s_eCreateUV && UV_SOLDIER_3 != s_pLogic->s_eCreateUV))
        {
            return ;
        }
        // attack cell.
        bool res = s_pLogic->GetRule()->Attack(0, s_pLogic->s_eSelUnitID, pTemp->GetID()) ;
        if (res)
        {
            // sound.
            if (s_bPlaySound)
            {
                if (-1 == pTemp->GetUnitID())
                {
                    SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                }
                else
                {
                    CE7Unit* pSoundUnit = s_pLogic->GetSelectUnitInfo(pTemp->GetUnitID()) ;
                    if (UV_CITY == pSoundUnit->GetUV())
                    {
                        SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_city) ;
                    }else if (UV_SOLDIER_1 == pSoundUnit->GetUV() ||
                              UV_SOLDIER_2 == pSoundUnit->GetUV() ||
                              UV_SOLDIER_3 == pSoundUnit->GetUV())
                    {
                        SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_soldier) ;
                    }
                    else // if (UV_TREE == pSoundUnit->GetUV())
                    {
                        SimpleAudioEngine::sharedEngine()->playEffect(sound_attack_wood) ;
                    }
                }
            }

            s_pLogic->s_eSelUnitID = -1 ;
            s_pLogic->s_eSelectUV = UV_NONE ;
        }
        else
        {
            if (s_bPlaySound)
            {
                SimpleAudioEngine::sharedEngine()->playEffect(sound_unconsid) ;
            }
        }
        return ;
    }

    printf("---- SingleTouchedEvent 8\n") ;
    if (s_pLogic->s_pSelect->GetTownID() != pTemp->GetTownID())
    {
        s_pLogic->s_pSelect = pTemp ;
        return ;
    }
    
    printf("---- SingleTouchedEvent 9\n") ;
    if (-1 != s_pLogic->s_eSelUnitID)
    {
//        printf("debug enter ---- 1 \n") ;
        // check attach unit.
        if (-1 != pTemp->GetUnitID())
        {
//            printf("debug enter ---- 2 \n") ;
            if (s_pLogic->s_eSelUnitID == pTemp->GetUnitID())
            {
//                printf("debug enter ---- 3 \n") ;
                CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
                pUnit->SetShowState(true) ;
                s_pLogic->s_eSelUnitID = -1 ;
                s_pLogic->s_eSelectUV = UV_NONE ;
            }
            else
            {
                int res = s_pLogic->GetRule()->AttachUnit(pTemp->GetUnitID(), s_pLogic->s_eSelUnitID) ;
                printf("AttachUnit res : [%d]\n", res) ;
            }
        }
        else
        {
//            printf("debug enter ---- 6 \n") ;
            CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
            CE7Cell* pOldCell = s_pLogic->ActiveMap()->GetSelectCellInfo(pUnit->GetCellID()) ;
            pOldCell->SetUnitID(-1) ;
            pOldCell->UpdateUV() ;

            pUnit->SetCellID(pTemp->GetID()) ;
            pUnit->SetShowState(true) ;
            pTemp->SetUnitID(pUnit->GetID()) ;
            pTemp->UpdateUV() ;

            s_pLogic->s_eSelUnitID = -1 ;
            s_pLogic->s_eSelectUV = UV_NONE ;
        }
        return ;
    }
    
    printf("---- SingleTouchedEvent 10\n") ;
    if (-1 != pTemp->GetUnitID())
    {
//        printf("debug enter ---- 7 \n") ;
        CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(pTemp->GetUnitID()) ;
        if (pUnit->IsActioned())
        {
            return ;
        }

        if (UV_SOLDIER_1 == pUnit->GetUV() ||
            UV_SOLDIER_2 == pUnit->GetUV() ||
            UV_SOLDIER_3 == pUnit->GetUV())
        {
//            printf("debug enter ---- 8 \n") ;
            s_pLogic->s_eSelUnitID = pUnit->GetID() ;
            s_pLogic->s_eSelectUV = pUnit->GetUV() ;
            pUnit->SetShowState(false) ;
        }
    }
    printf("---- SingleTouchedEvent E\n") ;
}

void DoubleTouchedEvent(CCPoint offset, float scale, CCPoint pos)
{
    printf("enter DoubleTouchedEvent === \n") ;
}

CE7GameLayer::CE7GameLayer()
{
    setIsTouchEnabled(true) ;

    m_eDS = DS_NONE ;

    CCSize s = CCDirector::sharedDirector()->getWinSize() ;
    m_pOpenGLLayer = new CE7OpenGLLayer() ;
    m_pGameChildLayer = new CE7GameChildLayer() ;
    m_pBackLayer = new CE7Layer() ;
    addChild(m_pBackLayer) ;

    if (IsPad())
    {
        CE7Sprite* pB = new CE7Sprite() ;
        pB->Init("back_game.png") ;
        
        m_pBackLayer->addChild(pB) ;
        m_pGameChildLayer->InitBackSheet("back_game1.png", 1, 2, 2, m_pBackLayer) ;
        m_pGameChildLayer->InitCellSheet("cell.png", 1000, 10, 10) ;
        m_pGameChildLayer->InitUnitSheet("unit.png", 1000, 12, 12) ;
        m_pGameChildLayer->InitArrowSheet("arrow.png", 500, 13, 13) ;
        m_pGameChildLayer->addChild(m_pOpenGLLayer, 11, 11) ;
        m_pGameChildLayer->setPosition(ccp(0.0f, 0.0f)) ;
        
        m_pZoomLayer = new CE7ZoomLayer() ;
        m_pZoomLayer->RegCallBackFunc(SingleTouchedEvent, DoubleTouchedEvent) ;
        m_pZoomLayer->LoadRes(m_pGameChildLayer, CCSizeMake(1024, 768), m_pBackLayer) ;
        
        addChild(m_pZoomLayer) ;
        addChild(m_pGameChildLayer) ;
        
        // add ui.
        m_pUILayer = new CCLayer() ;
        addChild(m_pUILayer) ;
        
        m_pBorderSprite = new CE7Sprite() ;
        m_pBorderSprite->Init("border.png") ;
        m_pUILayer->addChild(m_pBorderSprite) ;
        
        m_pButtonLose = CCMenuItemImage::itemFromNormalImage("button_lose_n.png", "button_lose_s.png", this,
                                                             menu_selector(CE7GameLayer::ClickedLose)) ;
        m_pButtonLose->setPosition(ccp(-450, -320)) ;
        m_pButtonHelp = CCMenuItemImage::itemFromNormalImage("button_help_n.png", "button_help_s.png", this,
                                                             menu_selector(CE7GameLayer::ClickedHelp)) ;
        m_pButtonHelp->setPosition(ccp(-380, -320)) ;
        m_pButtonTurnEnd = CCMenuItemImage::itemFromNormalImage("button_turnend_n.png", "button_turnend_s.png", this,
                                                                menu_selector(CE7GameLayer::ClickedTurnEnd)) ;
        m_pButtonTurnEnd->setPosition(ccp(400, -340)) ;
        m_pBorderButtons = CCMenu::menuWithItems(m_pButtonLose, m_pButtonHelp, m_pButtonTurnEnd, NULL) ;
        m_pUILayer->addChild(m_pBorderButtons) ;

        m_pTurnIDSheet = CCSpriteBatchNode::batchNodeWithFile("number_none.png") ;
        m_pUILayer->addChild(m_pTurnIDSheet) ;
        
        m_pTurnCountSheet = CCSpriteBatchNode::batchNodeWithFile("number_none.png") ;
        m_pUILayer->addChild(m_pTurnCountSheet) ;
        
        m_pYourTurnSprite = CCSprite::spriteWithFile("your.png") ;
        m_pYourTurnSprite->setPosition(CCPointMake(450, 660)) ;
        m_pUILayer->addChild(m_pYourTurnSprite) ;
        
        m_pEnemyTurnSprite = CCSprite::spriteWithFile("enemy.png") ;
        m_pEnemyTurnSprite->setPosition(CCPointMake(445, 660)) ;
        m_pUILayer->addChild(m_pEnemyTurnSprite) ;
        
        m_pTurnCountSprite = CCSprite::spriteWithFile("turn.png") ;
        m_pTurnCountSprite->setPosition(CCPointMake(100, 660)) ;
        m_pUILayer->addChild(m_pTurnCountSprite) ;
        
        m_pTurnIDSprite = CCSprite::spriteWithFile("turn.png") ;
        m_pTurnIDSprite->setPosition(CCPointMake(550, 660)) ;
        m_pUILayer->addChild(m_pTurnIDSprite) ;
        ////////////////////////////////////////////
        
        // init number batchnode.
        m_pNumberNoneSheet = CCSpriteBatchNode::batchNodeWithFile("number_none.png") ;
        int w = 25 ;
        int h = 35 ;
        for (int i = 0; i < 10; ++i)
        {
            m_rcNumberNone[i] = CCRectMake(w*i, 0, w, h) ;
        }
        
        m_pOperationSheet = CCSpriteBatchNode::batchNodeWithFile("operation.png") ;
        w = 25 ;
        h = 25 ;
        for (int i = 0; i < 4; ++i)
        {
            m_rcOperation[i] = CCRectMake(w*i, 0, w, h) ;
        }
        
        m_pNumberGoldSheet   = CCSpriteBatchNode::batchNodeWithFile("number_gold.png") ;
        m_pNumberInGoldSheet = CCSpriteBatchNode::batchNodeWithFile("number_ingold.png") ;
        w = 20 ;
        h = 25 ;
        for (int i = 0; i < 10; ++i)
        {
            m_rcNumberGold[i] = CCRectMake(w*i, 0, w, h) ;
        }
    }
    else // iphone.
    {
        CE7Sprite* pB = new CE7Sprite() ;
        pB->Init("back_game_iphone.png") ;
        
        m_pBackLayer->addChild(pB) ;
        m_pGameChildLayer->InitBackSheet("back_game1_iphone.png", 1, 2, 2, m_pBackLayer) ;
        m_pGameChildLayer->InitCellSheet("cell_iphone.png", 1000, 10, 10) ;
        m_pGameChildLayer->InitUnitSheet("unit_iphone.png", 1000, 12, 12) ;
        m_pGameChildLayer->InitArrowSheet("arrow_iphone.png", 500, 13, 13) ;
        m_pGameChildLayer->addChild(m_pOpenGLLayer, 11, 11) ;
        m_pGameChildLayer->setPosition(ccp(0.0f, 0.0f)) ;
        
        m_pZoomLayer = new CE7ZoomLayer() ;
        m_pZoomLayer->RegCallBackFunc(SingleTouchedEvent, DoubleTouchedEvent) ;
        m_pZoomLayer->LoadRes(m_pGameChildLayer, CCSizeMake(480, 320), m_pBackLayer) ;
        
        addChild(m_pZoomLayer) ;
        addChild(m_pGameChildLayer) ;
        
        // add ui.
        m_pUILayer = new CCLayer() ;
        addChild(m_pUILayer) ;
        
        m_pBorderSprite = new CE7Sprite() ;
        m_pBorderSprite->Init("border_iphone.png") ;
        m_pUILayer->addChild(m_pBorderSprite) ;
        
        m_pButtonLose = CCMenuItemImage::itemFromNormalImage("button_lose_n_iphone.png",
                                                             "button_lose_s_iphone.png", this,
                                                             menu_selector(CE7GameLayer::ClickedLose)) ;
        m_pButtonLose->setPosition(ccp(-450/2+5.0f, -320/2+30.0f)) ;
        m_pButtonHelp = CCMenuItemImage::itemFromNormalImage("button_help_n_iphone.png",
                                                             "button_help_s_iphone.png", this,
                                                             menu_selector(CE7GameLayer::ClickedHelp)) ;
        m_pButtonHelp->setPosition(ccp(-380/2+5.0f, -320/2+30.0f)) ;
        m_pButtonTurnEnd = CCMenuItemImage::itemFromNormalImage("button_turnend_n_iphone.png",
                                                                "button_turnend_s_iphone.png", this,
                                                                menu_selector(CE7GameLayer::ClickedTurnEnd)) ;
        m_pBorderButtons = CCMenu::menuWithItems(m_pButtonLose, m_pButtonHelp, m_pButtonTurnEnd, NULL) ;
        m_pButtonTurnEnd->setPosition(ccp(400/2-15.0f, -340/2+30.0f)) ;
        m_pUILayer->addChild(m_pBorderButtons) ;

        m_pTurnIDSheet = CCSpriteBatchNode::batchNodeWithFile("number_none_iphone.png") ;
        m_pUILayer->addChild(m_pTurnIDSheet) ;
        
        m_pTurnCountSheet = CCSpriteBatchNode::batchNodeWithFile("number_none_iphone.png") ;
        m_pUILayer->addChild(m_pTurnCountSheet) ;
        
        m_pYourTurnSprite = CCSprite::spriteWithFile("your_iphone.png") ;
        m_pYourTurnSprite->setPosition(CCPointMake(200, 275)) ;
        m_pUILayer->addChild(m_pYourTurnSprite) ;
        
        m_pEnemyTurnSprite = CCSprite::spriteWithFile("enemy_iphone.png") ;
        m_pEnemyTurnSprite->setPosition(CCPointMake(200, 275)) ;
        m_pUILayer->addChild(m_pEnemyTurnSprite) ;
        
        m_pTurnCountSprite = CCSprite::spriteWithFile("turn_iphone.png") ;
        m_pTurnCountSprite->setPosition(CCPointMake(50, 275)) ;
        m_pUILayer->addChild(m_pTurnCountSprite) ;
        
        m_pTurnIDSprite = CCSprite::spriteWithFile("turn_iphone.png") ;
        m_pTurnIDSprite->setPosition(CCPointMake(270, 275)) ;
        m_pUILayer->addChild(m_pTurnIDSprite) ;
        ////////////////////////////////////////////

        // init number batchnode.
        m_pNumberNoneSheet = CCSpriteBatchNode::batchNodeWithFile("number_none_iphone.png") ;
        float w = 12.5 ;
        float h = 17.5 ;
        for (int i = 0; i < 10; ++i)
        {
            m_rcNumberNone[i] = CCRectMake(w*i, 0, w, h) ;
        }
        
        m_pOperationSheet = CCSpriteBatchNode::batchNodeWithFile("operation_iphone.png") ;
        w = 12.5f ;
        h = 12.5f ;
        for (int i = 0; i < 4; ++i)
        {
            m_rcOperation[i] = CCRectMake(w*i, 0, w, h) ;
        }
        
        m_pNumberGoldSheet   = CCSpriteBatchNode::batchNodeWithFile("number_gold_iphone.png") ;
        m_pNumberInGoldSheet = CCSpriteBatchNode::batchNodeWithFile("number_ingold_iphone.png") ;
        w = 10.0f ;
        h = 12.5f ;
        for (int i = 0; i < 10; ++i)
        {
            m_rcNumberGold[i] = CCRectMake(w*i, 0, w, h) ;
        }
    }

    //////////////////////////////////////////////////////////

    // add info layer.
    InitCreateInfo() ;
    //////////////////////////////////////////////////////////

    // add number batchnode to this.
    addChild(m_pNumberNoneSheet) ;
    addChild(m_pOperationSheet) ;
    addChild(m_pNumberGoldSheet) ;
    addChild(m_pNumberInGoldSheet) ;
    //////////////////////////////////////////////////////////

    // add turn info sprite.

    m_pDialogLayer = new CE7DialogLayer(this) ;
    addChild(m_pDialogLayer, DIALOG_TAG, DIALOG_TAG) ;

    m_pHelpLayer = new CE7HelpLayer(this) ;
    m_pHelpLayer->Hide() ;
    addChild(m_pHelpLayer) ;

    CreateRandomMap() ;
}

CE7GameLayer::~CE7GameLayer()
{
    removeAllChildrenWithCleanup(true) ;
}

void CE7GameLayer::CreateRandomMap()
{
    s_pLogic->CreateStartMap() ;
    m_pGameChildLayer->Render() ;
    printf("debung --- game layer ---- [%f][%f]\n", s_pLogic->ActiveMap()->GetMaxSize().width,
           s_pLogic->ActiveMap()->GetMaxSize().height) ;
    m_pZoomLayer->SetTextureSize(s_pLogic->ActiveMap()->GetMaxSize()) ;
    m_pZoomLayer->Reset() ;

    s_pLogic->s_pSelect = NULL ;
}

void CE7GameLayer::ClickedLose()
{
    printf("enter ClickedLose function \n") ;
    if (s_bPlaySound)
    {
        SimpleAudioEngine::sharedEngine()->playEffect(sound_button) ;
    }

    if (!s_pLogic->IsUserTurn())
    {
        return ;
    }

    if (m_pDialogLayer->getIsVisible())
    {
        return ;
    }

    m_pDialogLayer->SetLose() ;
    m_pDialogLayer->ShowYESNO() ;
    m_eDS = DS_LOSE ;

    m_pZoomLayer->setIsTouchEnabled(false) ;
    setIsTouchEnabled(false) ;
}

void CE7GameLayer::ClickedHelp()
{
    if (s_bPlaySound)
    {
        SimpleAudioEngine::sharedEngine()->playEffect(sound_button) ;
    }

    if (!s_pLogic->IsUserTurn())
    {
        return ;
    }

    m_pHelpLayer->Show() ;

    m_pZoomLayer->setIsTouchEnabled(false) ;
    setIsTouchEnabled(false) ;
    
}

void CE7GameLayer::ClickedTurnEnd()
{
    printf("enter ClickedTurnEnd function \n") ;
    if (s_bPlaySound)
    {
        SimpleAudioEngine::sharedEngine()->playEffect(sound_button) ;
    }

    if (!s_pLogic->IsUserTurn())
    {
        return ;
    }

//    if (m_pDialogLayer->getIsVisible())
//    {
//        return ;
//    }

    s_pLogic->SetTurnID(1) ;
    s_pLogic->s_pSelect = NULL ;
    HideInfoLayer() ;

    bool flag = false ;
    if (flag)
    {
//        m_pDialogLayer->SetTurnEnd() ;
//        m_pDialogLayer->ShowYESNO() ;
//        m_eDS = DS_TURNEND ;
//        m_pZoomLayer->setIsTouchEnabled(false) ;
//        setIsTouchEnabled(false) ;
    }
    else
    {
        m_pZoomLayer->setIsTouchEnabled(true) ;
        setIsTouchEnabled(true) ;
    }
}

void CE7GameLayer::ClickedCreateSoldier()
{
    if (s_bPlaySound)
    {
        SimpleAudioEngine::sharedEngine()->playEffect(sound_button) ;
    }

    if (!s_pLogic->IsUserTurn())
    {
        return ;
    }

    s_pLogic->s_bIsRedraw = true ;
    s_pLogic->s_bIsUpdateInfo = true ;

//    printf("enter ClickedCreateSoldier function ==\n") ;
    if (0 >= s_pLogic->s_nSoldierCount)
    {
        return ;
    }

    if (UV_NONE == s_pLogic->s_eCreateUV)
    {
        if (UV_SOLDIER_2 >= s_pLogic->s_eSelectUV)
        {
            s_pLogic->s_eCreateUV = UV_SOLDIER_1 ;
            s_pLogic->s_nSoldierSub = 1 ;
        }
        // todo.
    }
    else if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV)
    {
        if (UV_SOLDIER_1 >= s_pLogic->s_eSelectUV)
        {
            s_pLogic->s_eCreateUV = UV_SOLDIER_2 ;
            s_pLogic->s_nSoldierSub = 2 ;
        }
        // todo.
    }
    else if (UV_SOLDIER_2 == s_pLogic->s_eCreateUV)
    {
        if (UV_NONE == s_pLogic->s_eSelectUV)
        {
            s_pLogic->s_eCreateUV = UV_SOLDIER_3 ;
            s_pLogic->s_nSoldierSub = 3 ;
        }
        // todo.
    }
    else if (UV_TOWER == s_pLogic->s_eCreateUV)
    {
        s_pLogic->s_eCreateUV = UV_SOLDIER_1 ;
        s_pLogic->s_nTowerSub = 0 ;

        if (-1 != s_pLogic->s_eSelUnitID)
        {
            CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
            pUnit->SetShowState(true) ;
            s_pLogic->s_eSelUnitID = -1 ;
            s_pLogic->s_eSelectUV = UV_NONE ;
        }
    }
    else if (UV_SOLDIER_3 == s_pLogic->s_eCreateUV)
    {
        return ;
    }
    else
    {
//        return ;
    }

    ShowInfoLayer() ;
}

void CE7GameLayer::ClickedCreateTower()
{
    if (s_bPlaySound)
    {
        SimpleAudioEngine::sharedEngine()->playEffect(sound_button) ;
    }

    if (!s_pLogic->IsUserTurn())
    {
        return ;
    }

    s_pLogic->s_bIsRedraw = true ;
    s_pLogic->s_bIsUpdateInfo = true ;

    printf("enter ClickedCreateTower function ==\n") ;
    if (0 >= s_pLogic->s_nTowerCount)
    {
        return ;
    }

    if (UV_NONE == s_pLogic->s_eCreateUV)
    {
        s_pLogic->s_nTowerSub = 1 ;
    }
    else if (UV_TOWER == s_pLogic->s_eCreateUV)
    {
        return ;
    }
    else
    {
        s_pLogic->s_nSoldierSub = 0 ;

        if (-1 != s_pLogic->s_eSelUnitID)
        {
            CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(s_pLogic->s_eSelUnitID) ;
            pUnit->SetShowState(true) ;
            s_pLogic->s_eSelUnitID = -1 ;
            s_pLogic->s_eSelectUV = UV_NONE ;
        }
    }

    s_pLogic->s_eCreateUV = UV_TOWER ;

    ShowInfoLayer() ;
}

//void CE7GameLayer::ClickedUndo()
//{
//    printf("enter ClickedUndo function \n") ;
//}

void CE7GameLayer::DialogYES()
{
//    printf("enter DialogYES function \n") ;
    s_pLogic->SetTurnID(1) ;
    s_pLogic->s_pSelect = NULL ;
    HideInfoLayer() ;
//    m_pZoomLayer->Reset() ;
    switch (m_eDS)
    {
        case DS_LOSE:
        {
            printf("enter CE7GameLayer::DialogYES DS_LOSE\n") ;
            SetStep(LS_STOP) ;
        } break ;
        case DS_GAMEEND:
        {
            printf("enter CE7GameLayer::DialogYES DS_GAMEEND\n") ;
            SetStep(LS_STOP) ;
        } break ;
        case DS_RETRY:
        {
            printf("logic info - 1: [%d][%d][%d]\n", s_pLogic->GetGameMode(), s_pLogic->GetStageMap(), s_pLogic->GetCustomMapID()) ;
            SetStep(LS_STOP) ;
            s_pLogic->s_bIsRestart = true ;
        } break ;
        case DS_NEXT:
        {
            printf("logic info - 2: [%d][%d][%d]\n", s_pLogic->GetGameMode(), s_pLogic->GetStageMap(), s_pLogic->GetCustomMapID()) ;
            SetStep(LS_STOP) ;
            if (STAGE_COUNT <= s_pLogic->GetStageMap()+1)
            {
            }
            else
            {
                s_pLogic->SetStageMap(s_pLogic->GetStageMap()+1) ;
                s_pLogic->s_bIsRestart = true ;
            }
        } break ;
        case DS_CONTINUE:
        {
            printf("logic info - 3: [%d][%d][%d]\n", s_pLogic->GetGameMode(), s_pLogic->GetStageMap(), s_pLogic->GetCustomMapID()) ;
            SetStep(LS_STOP) ;
            s_pLogic->s_bIsRestart = true ;
        } break ;
        default:
            break;
    }
}

void CE7GameLayer::DialogNO()
{
    printf("enter DialogNO function \n") ;

    switch (m_eDS)
    {
        case DS_NONE:
        {
        }break;
        case DS_LOSE:
        {
        } break ;
        case DS_TURNEND:
        {
        } break ;
        case DS_UNDO:
        {
        } break ;
        case DS_GAMEEND:
        {
        } break ;
        case DS_RETRY:
        {
            SetStep(LS_STOP) ;
        } break ;
        case DS_NEXT:
        {
            SetStep(LS_STOP) ;
        } break ;
        case DS_CONTINUE:
        {
            SetStep(LS_STOP) ;
        } break ;
        default:
            break;
    }

    m_pZoomLayer->setIsTouchEnabled(true) ;
    setIsTouchEnabled(true) ;
}

void CE7GameLayer::DialogOK()
{
    printf("enter DialogOK function \n") ;

    m_pZoomLayer->setIsTouchEnabled(true) ;
    setIsTouchEnabled(true) ;

    switch (m_eDS)
    {
        case DS_NONE:
        {
        }break;
        case DS_LOSE:
        {
            printf("enter CE7GameLayer::DialogOK DS_LOSE\n") ;
            SetStep(LS_STOP) ;
        } break ;
        case DS_TURNEND:
        {
        } break ;
        case DS_UNDO:
        {
        } break ;
        case DS_GAMEEND:
        {
            m_pZoomLayer->setIsTouchEnabled(false) ;
            setIsTouchEnabled(false) ;
            if (0 == s_pLogic->GetRule()->GetWinnerID())
            {
                if (0 == s_pLogic->GetGameMode())
                {
                    m_eDS = DS_NEXT ;
                    m_pDialogLayer->SetNext() ;
                    m_pDialogLayer->ShowYESNO() ;
                }
                else
                {
                    m_eDS = DS_CONTINUE ;
                    m_pDialogLayer->SetContinue() ;
                    m_pDialogLayer->ShowYESNO() ;
                }
            }
            else
            {
                if (0 == s_pLogic->GetGameMode())
                {
                    m_eDS = DS_RETRY ;
                    m_pDialogLayer->SetRetry() ;
                    m_pDialogLayer->ShowYESNO() ;
                }
                else
                {
                    m_eDS = DS_CONTINUE ;
                    m_pDialogLayer->SetContinue() ;
                    m_pDialogLayer->ShowYESNO() ;
                }
            }
        } break ;
        default:
            break;
    }
}

bool CE7GameLayer::Loop()
{
//    printf("m_eStep : ========== [%d]\n", GetStep()) ;
    if (LS_STOP == GetStep())
    {
        printf("end game layer [%d]\n", m_eDS) ;
        return false ;
    }

    if (m_pDialogLayer->getIsVisible())
    {
//        printf("m_pDialogLayer->getIsVisible() ====\n") ;
        return true ;
    }

    if (s_pLogic->IsGameEnd())
    {
//        printf("game layer loop ----\n") ;
        if (0 == s_pLogic->GetRule()->GetWinnerID())
        {
            CE7StageData::GetStageData()->SetStageLevel(s_pLogic->ActiveMap()->GetID(), 3) ;
            m_pDialogLayer->SetVictory() ;
            if (s_bPlaySound)
            {
                SimpleAudioEngine::sharedEngine()->playEffect(sound_win) ;
            }
        }
        else
        {
            m_pDialogLayer->SetFailure() ;
        }

        m_eDS = DS_GAMEEND ;
        m_pDialogLayer->ShowOK() ;
        return true ;
    }

    if (!s_pLogic->IsUserTurn())
    {
        s_pLogic->s_bIsRedraw = s_pLogic->Loop() ;
    }

    if (s_pLogic->s_bIsUpdateInfo)
    {
        ShowInfoLayer() ;
    }

    if (s_pLogic->s_bIsRedraw)
    {
        m_pGameChildLayer->Render() ;
        s_pLogic->s_bIsRedraw = false ;
    }

    UpdateTurnInfo(s_pLogic->GetTurnID(),s_pLogic->GetTurnCount()) ;

    return true ;
}

void CE7GameLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it ;
	CCTouch* touch ;
    
	for (it = pTouches->begin(); it != pTouches->end(); ++it)
	{
		touch = (CCTouch*)(*it) ;
		if (!touch)
        {
            break ;
        }
		CCPoint location = touch->locationInView(touch->view()) ;
		location = CCDirector::sharedDirector()->convertToGL(location) ;
	}
}

void CE7GameLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it ;
	CCTouch** touch = (CCTouch**)malloc(sizeof(CCTouch*)) ;
    int index = 0 ;
    
	for (it = pTouches->begin(); it != pTouches->end(); ++it) 
	{
		touch[index] = (CCTouch*)(*it) ;
        
		if (!touch[index])
        {
            break ;
        }
        ++index ;
	}
}

void CE7GameLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it ;
	CCTouch* touch ;
    
	for (it = pTouches->begin(); it != pTouches->end(); ++it) 
	{
		touch = (CCTouch*)(*it) ;
        
		if (!touch)
        {
            break ;
        }
        
		CCPoint location = touch->locationInView(touch->view()) ;
		location = CCDirector::sharedDirector()->convertToGL(location) ;
	}
}

void CE7GameLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
    printf("CE7GameLayer::ccTouchesCancelled\n") ;
}

void CE7GameLayer::SetPreviewMode()
{
    setAnchorPoint(ccp(0.5f, 0.5f)) ;
    setScale(0.4f) ;
    if (IsPad())
    {
        setPosition(ccp(250, 190)) ;
    }
    else
    {
        setPosition(ccp(250/2-30.0f, 190/2+60.0f)) ;
    }
    m_pBorderSprite->setIsVisible(false) ;
    m_pButtonTurnEnd->setIsVisible(false) ;
    m_pButtonLose->setIsVisible(false) ;
    m_pButtonHelp->setIsVisible(false) ;
    m_pBackLayer->setIsVisible(false) ;
    m_pUILayer->setIsVisible(false) ;

    m_pZoomLayer->setIsTouchEnabled(false) ;

    setIsTouchEnabled(false) ;
}

void CE7GameLayer::SetGameMode()
{
    SetStep(LS_START) ;

    setAnchorPoint(ccp(0, 0)) ;
    setScale(1.0f) ;
    setPosition(ccp(0, 0)) ;
    m_pBorderSprite->setIsVisible(true) ;
    m_pButtonTurnEnd->setIsVisible(true) ;
    m_pButtonLose->setIsVisible(true) ;
    m_pButtonHelp->setIsVisible(true) ;
    m_pBackLayer->setIsVisible(true) ;
    m_pUILayer->setIsVisible(true) ;

//    if (IsPad())
//    {
//        m_pLabelTurnCount = CCLabelTTF::labelWithString("Turn 0", "Marker Felt", 25) ;
//        m_pLabelTurnCount->setPosition(CCPointMake(112.0f, 660.0f)) ;
//        addChild(m_pLabelTurnCount) ;
//        
//        m_pLabelTurnID = CCLabelTTF::labelWithString("Your Time", "Marker Felt", 25) ;
//        m_pLabelTurnID->setPosition(CCPointMake(512.0f, 660.0f)) ;
//        addChild(m_pLabelTurnID) ;
//    }
//    else
//    {
//        m_pLabelTurnCount = CCLabelTTF::labelWithString("Turn 0", "Marker Felt", 11) ;
//        m_pLabelTurnCount->setPosition(CCPointMake(112.0f/2-30.0f, 660.0f/2-60.0f)) ;
//        addChild(m_pLabelTurnCount) ;
//        
//        m_pLabelTurnID = CCLabelTTF::labelWithString("Your Turn", "Marker Felt", 11) ;
//        m_pLabelTurnID->setPosition(CCPointMake(512.0f/2-30.0f, 660.0f/2-60.0f)) ;
//        addChild(m_pLabelTurnID) ;
//    }

    m_pZoomLayer->setIsTouchEnabled(true) ;
    setIsTouchEnabled(true) ;
}

void CE7GameLayer::InitCreateInfo()
{
    if (IsPad())
    {
        m_pInfoLayer = new CCLayer() ;
        addChild(m_pInfoLayer) ;
        
        CCSprite* pSoldierSprite = CCSprite::spriteWithFile("unit.png", CCRectMake(MAP_UNIT_WIDTH*3, 0, MAP_UNIT_WIDTH, MAP_UNIT_HEIGHT)) ;
        CCSprite* pTowerSprite = CCSprite::spriteWithFile("unit.png", CCRectMake(MAP_UNIT_WIDTH*6, 0, MAP_UNIT_WIDTH, MAP_UNIT_HEIGHT)) ;
        pTowerSprite->setScaleX(1.5f) ;
        
        m_pSoldierItem = CCMenuItemImage::itemFromNormalSprite(pSoldierSprite,
                                                               NULL,
                                                               this,
                                                               menu_selector(CE7GameLayer::ClickedCreateSoldier)) ;
        m_pTowerItem   = CCMenuItemImage::itemFromNormalSprite(pTowerSprite,
                                                               NULL,
                                                               this,
                                                               menu_selector(CE7GameLayer::ClickedCreateTower)) ;
        
        m_pSoldierItem->setPosition(CCPointMake(390, 270)) ;
        m_pTowerItem->setPosition(CCPointMake(440, 270)) ; // scaleX = 1.5f;
        
        m_pInfoMenus = CCMenu::menuWithItems(m_pSoldierItem, m_pTowerItem, NULL) ;
        
        m_pInfoLayer->addChild(m_pInfoMenus) ;
        
        // soldier operation.
        m_pSoldierOperation = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[2]) ;
        m_pSoldierOperation->setPosition(CCPointMake(880, 610)) ;
        m_pOperationSheet->addChild(m_pSoldierOperation) ;
        // soldier number.
        m_pSoldierNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pSoldierNumber[0]->setPosition(CCPointMake(m_pSoldierOperation->getPosition().x+20, m_pSoldierOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[0]) ;
        m_pSoldierNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pSoldierNumber[1]->setPosition(CCPointMake(m_pSoldierNumber[0]->getPosition().x+20, m_pSoldierNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[1]) ;
        
        // tower operation.
        m_pTowerOperation = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[2]) ;
        m_pTowerOperation->setPosition(CCPointMake(955, 610)) ;
        m_pOperationSheet->addChild(m_pTowerOperation) ;
        // tower number.
        m_pTowerNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pTowerNumber[0]->setPosition(CCPointMake(m_pTowerOperation->getPosition().x+20, m_pTowerOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[0]) ;
        m_pTowerNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pTowerNumber[1]->setPosition(CCPointMake(m_pTowerNumber[0]->getPosition().x+20, m_pTowerNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[1]) ;
        
        for (int i = 0; i < 2; ++i)
        {
            m_pOperationGold[i] = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[i]) ;
            m_pOperationInGold[i] = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[i]) ;
            m_pOperationGold[i]->setIsVisible(false) ;
            m_pOperationInGold[i]->setIsVisible(false) ;
            m_pOperationSheet->addChild(m_pOperationGold[i]) ;
            m_pOperationSheet->addChild(m_pOperationInGold[i]) ;
        }
    }
    else // iphone.
    {
        m_pInfoLayer = new CCLayer() ;
        addChild(m_pInfoLayer) ;
        
        CCSprite* pSoldierSprite = CCSprite::spriteWithFile("unit_iphone.png", CCRectMake(MAP_UNIT_WIDTH_IPHONE*3, 0, MAP_UNIT_WIDTH_IPHONE, MAP_UNIT_HEIGHT_IPHONE)) ;
        CCSprite* pTowerSprite = CCSprite::spriteWithFile("unit_iphone.png", CCRectMake(MAP_UNIT_WIDTH_IPHONE*6, 0, MAP_UNIT_WIDTH_IPHONE, MAP_UNIT_HEIGHT_IPHONE)) ;
        pTowerSprite->setScaleX(1.5f) ;
        
        m_pSoldierItem = CCMenuItemImage::itemFromNormalSprite(pSoldierSprite,
                                                               NULL,
                                                               this,
                                                               menu_selector(CE7GameLayer::ClickedCreateSoldier)) ;
        m_pTowerItem   = CCMenuItemImage::itemFromNormalSprite(pTowerSprite,
                                                               NULL,
                                                               this,
                                                               menu_selector(CE7GameLayer::ClickedCreateTower)) ;
        
        m_pSoldierItem->setPosition(CCPointMake(390/2-15.0f, 270/2-20.0f)) ;
        m_pTowerItem->setPosition(CCPointMake(440/2-15.0f, 270/2-20.0f)) ; // scaleX = 1.5f;
        
        m_pInfoMenus = CCMenu::menuWithItems(m_pSoldierItem, m_pTowerItem, NULL) ;
        
        m_pInfoLayer->addChild(m_pInfoMenus) ;
        
        // soldier operation.
        m_pSoldierOperation = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[2]) ;
        m_pSoldierOperation->setPosition(CCPointMake(880/2-30.0f, 610/2-50.0f)) ;
        m_pOperationSheet->addChild(m_pSoldierOperation) ;
        // soldier number.
        m_pSoldierNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pSoldierNumber[0]->setPosition(CCPointMake(m_pSoldierOperation->getPosition().x+20/2,
                                                     m_pSoldierOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[0]) ;
        m_pSoldierNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pSoldierNumber[1]->setPosition(CCPointMake(m_pSoldierNumber[0]->getPosition().x+20/2,
                                                     m_pSoldierNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[1]) ;

        // tower operation.
        m_pTowerOperation = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[2]) ;
        m_pTowerOperation->setPosition(CCPointMake(955/2-30.0f, 610/2-50.0f)) ;
        m_pOperationSheet->addChild(m_pTowerOperation) ;
        // tower number.
        m_pTowerNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pTowerNumber[0]->setPosition(CCPointMake(m_pTowerOperation->getPosition().x+20/2,
                                                   m_pTowerOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[0]) ;
        m_pTowerNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[0]) ;
        m_pTowerNumber[1]->setPosition(CCPointMake(m_pTowerNumber[0]->getPosition().x+20/2,
                                                   m_pTowerNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[1]) ;
        
        for (int i = 0; i < 2; ++i)
        {
            m_pOperationGold[i] = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[i]) ;
            m_pOperationInGold[i] = CCSprite::spriteWithBatchNode(m_pOperationSheet, m_rcOperation[i]) ;
            m_pOperationGold[i]->setIsVisible(false) ;
            m_pOperationInGold[i]->setIsVisible(false) ;
            m_pOperationSheet->addChild(m_pOperationGold[i]) ;
            m_pOperationSheet->addChild(m_pOperationInGold[i]) ;
        }
    }

    HideInfoLayer() ;
}

void CE7GameLayer::ShowInfoLayer()
{
    m_pOperationInGold[0]->setIsVisible(false) ;
    m_pOperationInGold[1]->setIsVisible(false) ;

    if (IsPad())
    {
        CCPoint showpos = CCPointMake(970, 530) ;
        s_pLogic->s_bIsUpdateInfo = false ;
        if (NULL == s_pLogic->s_pSelect)
        {
            return ;
        }
        
        m_pInfoLayer->removeChild(m_pSelSoldier, false) ;
        m_pInfoLayer->removeChild(m_pSelTower, false) ;
        
        if (UV_NONE == s_pLogic->s_eCreateUV)
        {
            s_pLogic->s_nSoldierSub = 0 ;
            s_pLogic->s_nTowerSub = 0 ;
            
            if (UV_NONE != s_pLogic->s_eSelectUV)
            {
                //            printf("enter debug info --- [%d]\n", s_eSelectUV-1) ;
                m_pSelSoldier = CCSprite::spriteWithFile("unit.png",
                                                         CCRectMake(MAP_UNIT_WIDTH*(s_pLogic->s_eSelectUV-1), 0, MAP_UNIT_WIDTH, MAP_UNIT_HEIGHT)) ;
                m_pSelSoldier->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelSoldier) ;
            }
        }
        else
        {
            if (UV_NONE != s_pLogic->s_eSelectUV)
            {
                eUnitValue uv = UV_NONE ;
                if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV && UV_SOLDIER_1 == s_pLogic->s_eSelectUV)
                {
                    uv = UV_SOLDIER_2 ;
                }
                else if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV && UV_SOLDIER_2 == s_pLogic->s_eSelectUV)
                {
                    uv = UV_SOLDIER_3 ;
                }
                else if (UV_SOLDIER_2 == s_pLogic->s_eCreateUV && UV_SOLDIER_1 == s_pLogic->s_eSelectUV)
                {
                    uv = UV_SOLDIER_3 ;
                }
                else
                {
                    printf("error ----- select uv [%d] , create uv [%d]\n", s_pLogic->s_eSelectUV, s_pLogic->s_eCreateUV) ;
                }
                m_pSelSoldier = CCSprite::spriteWithFile("unit.png", CCRectMake(MAP_UNIT_WIDTH*(uv-1), 0, MAP_UNIT_WIDTH, MAP_UNIT_HEIGHT)) ;
                m_pSelSoldier->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelSoldier) ;
            }
            else if (UV_TOWER == s_pLogic->s_eCreateUV)
            {
                m_pSelTower = CCSprite::spriteWithFile("unit.png", CCRectMake(MAP_UNIT_WIDTH*(s_pLogic->s_eCreateUV-1), 0, MAP_UNIT_WIDTH, MAP_UNIT_HEIGHT)) ;
                m_pSelTower->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelTower) ;
            }
            else
            {
                m_pSelSoldier = CCSprite::spriteWithFile("unit.png", CCRectMake(MAP_UNIT_WIDTH*(s_pLogic->s_eCreateUV-1), 0, MAP_UNIT_WIDTH, MAP_UNIT_HEIGHT)) ;
                m_pSelSoldier->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelSoldier) ;
            }
        }
        
        TOWN_INFO_LIST::iterator itr ;
        for (itr = s_pLogic->m_listTown.begin(); itr != s_pLogic->m_listTown.end(); ++itr)
        {
            if ((*itr)->GetID() == s_pLogic->s_pSelect->GetTownID())
            {
                //            printf("town id:[%d] gold:[%d] in:[%d] cells:[%lu] units:[%lu] countryid[%d]\n",
                //                   (*itr)->GetID(), (*itr)->GetGold(), (*itr)->GetInGold(),
                //                   (*itr)->m_listCellIDs.size(), (*itr)->m_listUnitIDs.size(), (*itr)->GetCountryID()) ;
                s_pLogic->s_nGold = (*itr)->GetGold() ;
                s_pLogic->s_nInGold = (*itr)->GetInGold() ;
                s_pLogic->s_nSoldierCount = (*itr)->GetGold()/(UNIT_NEED_GOLD_ARRAY[UV_SOLDIER_1]) ;
                s_pLogic->s_nTowerCount = (*itr)->GetGold()/(UNIT_NEED_GOLD_ARRAY[UV_TOWER]) ;
                break ;
            }
        }
        
        //    printf("CE7GameLayer::ShowInfoLayer info : [%d][%d][%d][%d]\n",
        //           s_nGold, s_nInGold, s_nSoldierCount, s_nTowerCount) ;
        
        m_pNumberNoneSheet->removeChild(m_pSoldierNumber[0], false) ;
        m_pNumberNoneSheet->removeChild(m_pSoldierNumber[1], false) ;
        
        m_pNumberNoneSheet->removeChild(m_pTowerNumber[0], false) ;
        m_pNumberNoneSheet->removeChild(m_pTowerNumber[1], false) ;
        
        int tempgold = (int)(s_pLogic->s_nGold-s_pLogic->s_nSoldierSub*UNIT_NEED_GOLD_ARRAY[UV_SOLDIER_1]-s_pLogic->s_nTowerSub*UNIT_NEED_GOLD_ARRAY[UV_TOWER]) ;
        int tempsoldier = (int)(tempgold/UNIT_NEED_GOLD_ARRAY[UV_SOLDIER_1]) ;
        int temptower = (int)(tempgold/UNIT_NEED_GOLD_ARRAY[UV_TOWER]) ;
        
        m_pSoldierNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(tempsoldier/10)]) ;
        m_pSoldierNumber[0]->setPosition(CCPointMake(m_pSoldierOperation->getPosition().x+20, m_pSoldierOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[0]) ;
        m_pSoldierNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(tempsoldier%10)]) ;
        m_pSoldierNumber[1]->setPosition(CCPointMake(m_pSoldierNumber[0]->getPosition().x+20, m_pSoldierNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[1]) ;
        
        m_pTowerNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(temptower/10)]) ;
        m_pTowerNumber[0]->setPosition(CCPointMake(m_pTowerOperation->getPosition().x+20, m_pTowerOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[0]) ;
        m_pTowerNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(temptower%10)]) ;
        m_pTowerNumber[1]->setPosition(CCPointMake(m_pTowerNumber[0]->getPosition().x+20, m_pTowerNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[1]) ;
        
        CE7Town* pSelTown = s_pLogic->GetSelectTownInfo(s_pLogic->s_pSelect->GetTownID()) ;
        int tg = pSelTown->GetGold() ;
        int tg_bit = 0 ;
        float gold_x = 312.0f ;
        float gold_y = 730.0f ;
        float offsetx = 20.0f ;
        
        m_pNumberGoldSheet->removeAllChildrenWithCleanup(false) ;
        m_pNumberInGoldSheet->removeAllChildrenWithCleanup(false) ;
        
        if (0 < tg)
        {
            while (0 < tg)
            {
                int index = tg % 10 ;
                tg = (int)(tg / 10) ;
                CCSprite* pGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[index]) ;
                pGold->setPosition(CCPointMake(gold_x+(tg_bit--)*offsetx, gold_y)) ;
                m_pNumberGoldSheet->addChild(pGold) ;
            }
            //        int oper = (0 <= tg) ? 0 : 1 ;
            //        m_pOperationGold[oper]->setPosition(CCPointMake(gold_x+tg_bit*offsetx, gold_y)) ;
            //        m_pOperationGold[oper]->setIsVisible(true) ;
        }
        else
        {
            CCSprite* pGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[0]) ;
            pGold->setPosition(CCPointMake(gold_x, gold_y)) ;
            m_pNumberGoldSheet->addChild(pGold) ;
        }

        int tig = pSelTown->GetInGold() ;
        int tig_bit = 0 ;
        float ingold_x = 972.0f ;
        float ingold_y = 730.0f ;
        if (0 < tig)
        {
            while (0 < tig)
            {
                int index = tig % 10 ;
                tig = (int)(tig / 10) ;
                CCSprite* pInGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[index]) ;
                pInGold->setPosition(CCPointMake(ingold_x+(tig_bit--)*offsetx, ingold_y)) ;
                m_pNumberGoldSheet->addChild(pInGold) ;
            }
            m_pOperationInGold[0]->setPosition(CCPointMake(ingold_x+tig_bit*offsetx, ingold_y)) ;
            m_pOperationInGold[0]->setIsVisible(true) ;
        }
        else if (0 == tig)
        {
            CCSprite* pGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[0]) ;
            pGold->setPosition(CCPointMake(ingold_x+(tig_bit--)*offsetx, ingold_y)) ;
            m_pNumberGoldSheet->addChild(pGold) ;
            
            m_pOperationInGold[0]->setPosition(CCPointMake(ingold_x+tig_bit*offsetx, ingold_y)) ;
            m_pOperationInGold[0]->setIsVisible(true) ;
        }
        else
        {
            tig = -tig ;
            while (0 < tig)
            {
                int index = tig % 10 ;
                tig = (int)(tig / 10) ;
                CCSprite* pInGold = CCSprite::spriteWithBatchNode(m_pNumberInGoldSheet, m_rcNumberGold[index]) ;
                pInGold->setPosition(CCPointMake(ingold_x+(tig_bit--)*offsetx, ingold_y)) ;
                m_pNumberInGoldSheet->addChild(pInGold) ;
            }
            m_pOperationInGold[1]->setPosition(CCPointMake(ingold_x+tig_bit*offsetx, ingold_y)) ;
            m_pOperationInGold[1]->setIsVisible(true) ;
        }
        
        if (!m_pInfoLayer->getIsVisible())
        {
            m_pOperationSheet->setIsVisible(true) ;
            m_pNumberNoneSheet->setIsVisible(true) ;
            m_pNumberGoldSheet->setIsVisible(true) ;
            m_pNumberInGoldSheet->setIsVisible(true) ;
            m_pInfoLayer->setIsVisible(true) ;
        }
    }
    else // iphone.
    {
        CCPoint showpos = CCPointMake(970/2-30.0f, 530/2-40.0f) ;
        s_pLogic->s_bIsUpdateInfo = false ;
        if (NULL == s_pLogic->s_pSelect)
        {
            return ;
        }
        
        m_pInfoLayer->removeChild(m_pSelSoldier, false) ;
        m_pInfoLayer->removeChild(m_pSelTower, false) ;
        
        if (UV_NONE == s_pLogic->s_eCreateUV)
        {
            s_pLogic->s_nSoldierSub = 0 ;
            s_pLogic->s_nTowerSub = 0 ;
            
            if (UV_NONE != s_pLogic->s_eSelectUV)
            {
                //            printf("enter debug info --- [%d]\n", s_eSelectUV-1) ;
                m_pSelSoldier = CCSprite::spriteWithFile("unit_iphone.png",
                                                         CCRectMake(MAP_UNIT_WIDTH_IPHONE*(s_pLogic->s_eSelectUV-1), 0,
                                                                    MAP_UNIT_WIDTH_IPHONE, MAP_UNIT_HEIGHT_IPHONE)) ;
                m_pSelSoldier->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelSoldier) ;
            }
        }
        else
        {
            if (UV_NONE != s_pLogic->s_eSelectUV)
            {
                eUnitValue uv = UV_NONE ;
                if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV && UV_SOLDIER_1 == s_pLogic->s_eSelectUV)
                {
                    uv = UV_SOLDIER_2 ;
                }
                else if (UV_SOLDIER_1 == s_pLogic->s_eCreateUV && UV_SOLDIER_2 == s_pLogic->s_eSelectUV)
                {
                    uv = UV_SOLDIER_3 ;
                }
                else if (UV_SOLDIER_2 == s_pLogic->s_eCreateUV && UV_SOLDIER_1 == s_pLogic->s_eSelectUV)
                {
                    uv = UV_SOLDIER_3 ;
                }
                else
                {
                    printf("error ----- select uv [%d] , create uv [%d]\n", s_pLogic->s_eSelectUV, s_pLogic->s_eCreateUV) ;
                }
                if (IsPad())
                {
                    m_pSelSoldier = CCSprite::spriteWithFile("unit.png", CCRectMake(MAP_UNIT_WIDTH_IPHONE*(uv-1), 0,
                                                                                    MAP_UNIT_WIDTH_IPHONE, MAP_UNIT_HEIGHT_IPHONE)) ;
                }
                else
                {
                    m_pSelSoldier = CCSprite::spriteWithFile("unit_iphone.png", CCRectMake(MAP_UNIT_WIDTH_IPHONE*(uv-1), 0,
                                                                                           MAP_UNIT_WIDTH_IPHONE,
                                                                                           MAP_UNIT_HEIGHT_IPHONE)) ;
                }
                m_pSelSoldier->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelSoldier) ;
            }
            else if (UV_TOWER == s_pLogic->s_eCreateUV)
            {
                m_pSelTower = CCSprite::spriteWithFile("unit_iphone.png", CCRectMake(MAP_UNIT_WIDTH_IPHONE*(s_pLogic->s_eCreateUV-1), 0, MAP_UNIT_WIDTH_IPHONE, MAP_UNIT_HEIGHT_IPHONE)) ;
                m_pSelTower->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelTower) ;
            }
            else
            {
                m_pSelSoldier = CCSprite::spriteWithFile("unit_iphone.png", CCRectMake(MAP_UNIT_WIDTH_IPHONE*(s_pLogic->s_eCreateUV-1), 0, MAP_UNIT_WIDTH_IPHONE, MAP_UNIT_HEIGHT_IPHONE)) ;
                m_pSelSoldier->setPosition(showpos) ;
                m_pInfoLayer->addChild(m_pSelSoldier) ;
            }
        }
        
        TOWN_INFO_LIST::iterator itr ;
        for (itr = s_pLogic->m_listTown.begin(); itr != s_pLogic->m_listTown.end(); ++itr)
        {
            if ((*itr)->GetID() == s_pLogic->s_pSelect->GetTownID())
            {
                s_pLogic->s_nGold = (*itr)->GetGold() ;
                s_pLogic->s_nInGold = (*itr)->GetInGold() ;
                s_pLogic->s_nSoldierCount = (*itr)->GetGold()/(UNIT_NEED_GOLD_ARRAY[UV_SOLDIER_1]) ;
                s_pLogic->s_nTowerCount = (*itr)->GetGold()/(UNIT_NEED_GOLD_ARRAY[UV_TOWER]) ;
                break ;
            }
        }

        m_pNumberNoneSheet->removeChild(m_pSoldierNumber[0], false) ;
        m_pNumberNoneSheet->removeChild(m_pSoldierNumber[1], false) ;
        
        m_pNumberNoneSheet->removeChild(m_pTowerNumber[0], false) ;
        m_pNumberNoneSheet->removeChild(m_pTowerNumber[1], false) ;
        
        int tempgold = (int)(s_pLogic->s_nGold-s_pLogic->s_nSoldierSub*UNIT_NEED_GOLD_ARRAY[UV_SOLDIER_1]-s_pLogic->s_nTowerSub*UNIT_NEED_GOLD_ARRAY[UV_TOWER]) ;
        int tempsoldier = (int)(tempgold/UNIT_NEED_GOLD_ARRAY[UV_SOLDIER_1]) ;
        int temptower = (int)(tempgold/UNIT_NEED_GOLD_ARRAY[UV_TOWER]) ;
        
        m_pSoldierNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(tempsoldier/10)]) ;
        m_pSoldierNumber[0]->setPosition(CCPointMake(m_pSoldierOperation->getPosition().x+20/2, m_pSoldierOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[0]) ;
        m_pSoldierNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(tempsoldier%10)]) ;
        m_pSoldierNumber[1]->setPosition(CCPointMake(m_pSoldierNumber[0]->getPosition().x+20/2, m_pSoldierNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pSoldierNumber[1]) ;
        
        m_pTowerNumber[0] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(temptower/10)]) ;
        m_pTowerNumber[0]->setPosition(CCPointMake(m_pTowerOperation->getPosition().x+20/2, m_pTowerOperation->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[0]) ;
        m_pTowerNumber[1] = CCSprite::spriteWithBatchNode(m_pNumberNoneSheet, m_rcNumberNone[(int)(temptower%10)]) ;
        m_pTowerNumber[1]->setPosition(CCPointMake(m_pTowerNumber[0]->getPosition().x+20/2, m_pTowerNumber[0]->getPosition().y)) ;
        m_pNumberNoneSheet->addChild(m_pTowerNumber[1]) ;
        
        CE7Town* pSelTown = s_pLogic->GetSelectTownInfo(s_pLogic->s_pSelect->GetTownID()) ;
        int tg = pSelTown->GetGold() ;
        int tg_bit = 0 ;
        float gold_x = 312.0f/2 ;
        float gold_y = 730.0f/2-60.0f ;
        float offsetx = 20.0f/2 ;
        
        m_pNumberGoldSheet->removeAllChildrenWithCleanup(false) ;
        m_pNumberInGoldSheet->removeAllChildrenWithCleanup(false) ;
        
        if (0 < tg)
        {
            while (0 < tg)
            {
                int index = tg % 10 ;
                tg = (int)(tg / 10) ;
                CCSprite* pGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[index]) ;
                pGold->setPosition(CCPointMake(gold_x+(tg_bit--)*offsetx, gold_y)) ;
                m_pNumberGoldSheet->addChild(pGold) ;
            }
        }
        else
        {
            CCSprite* pGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[0]) ;
            pGold->setPosition(CCPointMake(gold_x, gold_y)) ;
            m_pNumberGoldSheet->addChild(pGold) ;
        }
        
        int tig = pSelTown->GetInGold() ;
        int tig_bit = 0 ;
        float ingold_x = 972.0f/2-25.0f ;
        float ingold_y = 730.0f/2-60.0f ;
        if (0 < tig)
        {
            while (0 < tig)
            {
                int index = tig % 10 ;
                tig = (int)(tig / 10) ;
                CCSprite* pInGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[index]) ;
                pInGold->setPosition(CCPointMake(ingold_x+(tig_bit--)*offsetx, ingold_y)) ;
                m_pNumberGoldSheet->addChild(pInGold) ;
            }
            m_pOperationInGold[0]->setPosition(CCPointMake(ingold_x+tig_bit*offsetx, ingold_y)) ;
            m_pOperationInGold[0]->setIsVisible(true) ;
        }
        else if (0 == tig)
        {
            CCSprite* pGold = CCSprite::spriteWithBatchNode(m_pNumberGoldSheet, m_rcNumberGold[0]) ;
            pGold->setPosition(CCPointMake(ingold_x+(tig_bit--)*offsetx, ingold_y)) ;
            m_pNumberGoldSheet->addChild(pGold) ;
            
            m_pOperationInGold[0]->setPosition(CCPointMake(ingold_x+tig_bit*offsetx, ingold_y)) ;
            m_pOperationInGold[0]->setIsVisible(true) ;
        }
        else
        {
            tig = -tig ;
            while (0 < tig)
            {
                int index = tig % 10 ;
                tig = (int)(tig / 10) ;
                CCSprite* pInGold = CCSprite::spriteWithBatchNode(m_pNumberInGoldSheet, m_rcNumberGold[index]) ;
                pInGold->setPosition(CCPointMake(ingold_x+(tig_bit--)*offsetx, ingold_y)) ;
                m_pNumberInGoldSheet->addChild(pInGold) ;
            }
            m_pOperationInGold[1]->setPosition(CCPointMake(ingold_x+tig_bit*offsetx, ingold_y)) ;
            m_pOperationInGold[1]->setIsVisible(true) ;
        }
        
        if (!m_pInfoLayer->getIsVisible())
        {
            m_pOperationSheet->setIsVisible(true) ;
            m_pNumberNoneSheet->setIsVisible(true) ;
            m_pNumberGoldSheet->setIsVisible(true) ;
            m_pNumberInGoldSheet->setIsVisible(true) ;
            m_pInfoLayer->setIsVisible(true) ;
        }
    }
}

void CE7GameLayer::HideInfoLayer()
{
    if(!m_pInfoLayer->getIsVisible())
    {
        return ;
    }

    for (int i = 0; i < 2; ++i)
    {
        m_pOperationGold[i]->setIsVisible(false) ;
        m_pOperationInGold[i]->setIsVisible(false) ;
    }

    m_pOperationSheet->setIsVisible(false) ;
    m_pNumberNoneSheet->setIsVisible(false) ;
    m_pNumberGoldSheet->setIsVisible(false) ;
    m_pNumberInGoldSheet->setIsVisible(false) ;
    m_pInfoLayer->setIsVisible(false) ;
}

void CE7GameLayer::UpdateTurnInfo(int turnid, int turncount)
{
    m_pTurnIDSheet->removeAllChildrenWithCleanup(false) ;
    m_pTurnCountSheet->removeAllChildrenWithCleanup(false) ;

    if (IsPad())
    {
        if (99 <= turncount)
        {
            CCSprite* pNumber1 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[9]) ;
            pNumber1->setPosition(CCPointMake(160, 660)) ;
            m_pTurnCountSheet->addChild(pNumber1) ;
            
            CCSprite* pNumber2 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[9]) ;
            pNumber2->setPosition(CCPointMake(180, 660)) ;
            m_pTurnCountSheet->addChild(pNumber2) ;
        }
        else
        {
            CCSprite* pNumber1 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[(int)(turncount/10)]) ;
            pNumber1->setPosition(CCPointMake(160, 660)) ;
            m_pTurnCountSheet->addChild(pNumber1) ;
            
            CCSprite* pNumber2 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[(int)(turncount%10)]) ;
            pNumber2->setPosition(CCPointMake(180, 660)) ;
            m_pTurnCountSheet->addChild(pNumber2) ;
        }
    }
    else
    {
        if (99 <= turncount)
        {
            CCSprite* pNumber1 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[9]) ;
            pNumber1->setPosition(CCPointMake(80, 275)) ;
            m_pTurnCountSheet->addChild(pNumber1) ;
            
            CCSprite* pNumber2 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[9]) ;
            pNumber2->setPosition(CCPointMake(90, 275)) ;
            m_pTurnCountSheet->addChild(pNumber2) ;
        }
        else
        {
            CCSprite* pNumber1 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[(int)(turncount/10)]) ;
            pNumber1->setPosition(CCPointMake(80, 275)) ;
            m_pTurnCountSheet->addChild(pNumber1) ;
            
            CCSprite* pNumber2 = CCSprite::spriteWithBatchNode(m_pTurnCountSheet, m_rcNumberNone[(int)(turncount%10)]) ;
            pNumber2->setPosition(CCPointMake(90, 275)) ;
            m_pTurnCountSheet->addChild(pNumber2) ;
        }
    }

    if (0 == turnid)
    {
        m_pYourTurnSprite->setIsVisible(true) ;
        m_pEnemyTurnSprite->setIsVisible(false) ;
    }
    else
    {
        m_pYourTurnSprite->setIsVisible(false) ;
        m_pEnemyTurnSprite->setIsVisible(true) ;

        if (IsPad())
        {
            CCSprite* pTurnIDNumber = CCSprite::spriteWithBatchNode(m_pTurnIDSheet, m_rcNumberNone[turnid]) ;
            pTurnIDNumber->setPosition(CCPointMake(500, 660)) ;
            m_pTurnIDSheet->addChild(pTurnIDNumber) ;
        }
        else
        {
            CCSprite* pTurnIDNumber = CCSprite::spriteWithBatchNode(m_pTurnIDSheet, m_rcNumberNone[turnid]) ;
            pTurnIDNumber->setPosition(CCPointMake(240, 275)) ;
            m_pTurnIDSheet->addChild(pTurnIDNumber) ;
        }
    }
}






















